package cn.dakaqi.welfare.services;

import cn.dakaqi.entities.Welfare;
import cn.dakaqi.entities.WelfareItem;
import cn.dakaqi.utils.DKQConstant;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chunyang on 2016/4/18.
 */
@Component(value = "welfareItemService")
@Transactional
public class WelfareItemServiceImpl implements WelfareItemService
{
    @Autowired
    WelfareItemDao welfareItemDao;
    @Autowired
    WelfareService welfareService;

    @Override
    public void save(String param) throws ServiceRuntimeException
    {
        JSONObject jsonObject = JSON.parseObject(param);
        String code = jsonObject.getString("code");
        Long welfareId = jsonObject.getLong("welfareId");
        Welfare welfare = this.welfareService.findOne(welfareId);
        WelfareItem welfareItem = new WelfareItem(code,welfare);
        save(welfareItem);
    }

    @Override
    public void save(WelfareItem welfareItem) throws ServiceRuntimeException
    {
        this.welfareItemDao.save(welfareItem);
    }

    @Override
    public void update(WelfareItem welfareItem) throws ServiceRuntimeException
    {
        this.welfareItemDao.save(welfareItem);
    }

    @Override
    public void updateIsChanage(Long id) throws ServiceRuntimeException
    {
        WelfareItem welfareItem = this.welfareItemDao.findOne(id);
        welfareItem.setIsChanage(1);
        welfareItemDao.save(welfareItem);
    }
    public void updateIsUse(Long id) throws ServiceRuntimeException
    {
        WelfareItem welfareItem = this.welfareItemDao.findOne(id);
        welfareItem.setIsUse(1);
        welfareItemDao.save(welfareItem);
    }
    @Override
    public Page<WelfareItem> findByWelfare(Long welfareId,int pageNumber) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(pageNumber - 1, 20, sort);

        Map<String,Object> map = new HashMap<String, Object>();
        //map.put("LIKE_NAME",name);
        //Map<String, SearchFilter> filters = SearchFilter.parse(map);

        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("Welfare.id", new SearchFilter("Welfare.id", SearchFilter.Operator.EQ, welfareId));
        Specification<WelfareItem> spec = DynamicSpecifications.bySearchFilter(filters.values(), WelfareItem.class);
        return this.welfareItemDao.findAll(spec, pageRequest);
    }

    @Override
    public WelfareItem findUnChangeByWelfare(Long welfareId) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("welfare.id", new SearchFilter("welfare.id", SearchFilter.Operator.EQ, welfareId));
        filters.put("isChanage", new SearchFilter("isChanage", SearchFilter.Operator.EQ, DKQConstant.WELFARE_ITEM_UNCHANGE));
        Specification<WelfareItem> spec = DynamicSpecifications.bySearchFilter(filters.values(), WelfareItem.class);
        List<WelfareItem> list = this.welfareItemDao.findAll(spec);
        if(null == list || list.size() ==0)
            return null;
        return list.get(0);
    }

    @Override
    public List<WelfareItem> findByMemberAndWelfare(String memberCode, Long welfareId) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("welfare.id", new SearchFilter("welfare.id", SearchFilter.Operator.EQ, welfareId));
        filters.put("memberCode", new SearchFilter("memberCode", SearchFilter.Operator.EQ, memberCode));
        Specification<WelfareItem> spec = DynamicSpecifications.bySearchFilter(filters.values(), WelfareItem.class);
        return this.welfareItemDao.findAll(spec);
    }
}
